import bean.Student;
import bean.Student02;
import com.github.pagehelper.PageHelper;
import dao.StudentDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 飞哥
 * @version V1.0
 * @package PACKAGE_NAME
 * @date 2021/7/15 9:47
 */
public class MyBatisTest01 {

    @Test
    public void selectStudentIf() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setName("张三");
        student.setAge(20);
        List<Student> list = dao.selectStudentIf(student);
        for (Student student1 : list) {
            System.out.println("信息：" + student1);
        }
    }

    @Test
    public void selectStudentWhere() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setAge(21);
        student.setName("张三");
        List<Student> list = dao.selectStudentWhere(student);
        for (Student student1 : list) {
            System.out.println("使用where标签查询学生信息的结果:" + student1);
        }
    }


    @Test
    /**
     * 以下效果的实现可以使用<foreach></foreach>标签
     */
    public void testFor() {
        List<Integer> list = new ArrayList<>();
        list.add(1001);
        list.add(1002);
        list.add(1003);

        //拼接字符串形成以下效果
        //String sql = "select * from student where id in(1001,1002,1003)";
        String sql = "select * from student where id in";
        StringBuilder builder = new StringBuilder();

        builder.append("(");
        for (Integer integer : list) {
            builder.append(integer).append(",");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append(");");
        sql += builder.toString();

        System.out.println(sql);
    }

    @Test
    public void selectForeachOne() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Integer> list = new ArrayList<>();
        list.add(1001);
        list.add(1002);
        list.add(1003);
        List<Student> students = dao.selectForeachOne(list);
        for (Student student : students) {
            System.out.println("使用foreach标签得到的学生信息：" + student);
        }
    }

    @Test
    public void selectForeachTwo() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001, null, null, null));
        list.add(new Student(1002, null, null, null));
        list.add(new Student(1003, null, null, null));
        List<Student> students = dao.selectForeachTwo(list);
        for (Student student : students) {
            System.out.println("2使用foreach标签得到的学生信息：" + student);
        }
    }

    @Test
    public void selectForeachThree() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student02> list = new ArrayList<>();
        list.add(new Student02(1001, null, null, null));
        list.add(new Student02(1002, null, null, null));
        list.add(new Student02(1003, null, null, null));
        List<Student02> students = dao.selectForeachThree(list);
        for (Student02 student : students) {
            System.out.println("3使用foreach标签得到的学生信息：" + student);
        }
    }

    @Test
    /**
     * 使用PageHelper类实现分页功能
     */
    public void pageHelperTest() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        //加入PageHelper的方法进行分页
        // pageNum:第几页，从1开始
        // pageSize:一页中有多少行数据
        PageHelper.startPage(2,2);//展示第二页数据，每页两行数据
        List<Student> list = dao.pageHelperTest();
        for (Student student : list) {
            System.out.println("使用pageHelper类实现分页功能的查询结果:" + student);
        }
        //关闭sqlSession对象
        sqlSession.close();
    }
}